//
//  PerfectCarInfoVC1.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/7/15.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "PerfectCarInfoVC1.h"
#import "TopBackView.h"
#import "CarView.h"
#import "PerfectCarInfoVC2.h"
#import "RootViewController.h"
#import "Common.h"
#import "QiniuSDK.h"
#import "WKProgressHUD.h"
#import <SDWebImage/UIImageView+WebCache.h>


@interface PerfectCarInfoVC1 ()<UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITextField *statusTextField;
@property (nonatomic,strong) UITextField *nameTextField;//姓名
@property (nonatomic,strong) UITextField *idTextField;//身份证号
//@property (nonatomic,strong) UITextField *driverNumberTF;//司机电话
//@property (nonatomic,strong) UITextField *familyNumberTF;//家庭电话
@property (nonatomic,strong) UITextField *addressTF;//家庭住址
@property (nonatomic,strong) UITextField *driverNumberTF;//籍贯
@property (nonatomic,strong) UITextField *driverDateTF;//出生日期




@property (nonatomic,strong) NSMutableDictionary *dic;
@property (nonatomic,strong) NSMutableDictionary *postDic;

@property (nonatomic,strong) UIButton *idFirstButton;
@property (nonatomic,strong) UIImageView *firstImg;
@property (nonatomic,copy) NSString *token1;
@property (nonatomic) NSInteger integer;

@property (nonatomic,strong) UIButton *idSecondButton;
@property (nonatomic,strong) UIImageView *secondImg;
@property (nonatomic,copy) NSString *token2;
@property (nonatomic,strong) WKProgressHUD *hud;


@property (nonatomic) int currentTextFieldIndex;
@property (nonatomic,strong) NSMutableArray *textFieldArray;

@property (nonatomic) int number;

@end

@implementation PerfectCarInfoVC1

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dic = [NSMutableDictionary dictionary];
    self.postDic = [NSMutableDictionary dictionary];
    self.textFieldArray = [NSMutableArray array];
    [self.postDic setObject:@"" forKey:@"idno"];
    [self.postDic setObject:@"" forKey:@"idpic1"];
    [self.postDic setObject:@"" forKey:@"idpic2"];
    [self.postDic setObject:@"" forKey:@"realName"];

    self.integer = 0;
    self.number = 0;
    self.firstImg = [[UIImageView alloc]init];
    self.secondImg = [[UIImageView alloc]init];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"司机认证"];
    
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    topBackView.rightButton.hidden = YES;
    
    [self.view addSubview:topBackView];
    
    self.view.backgroundColor = GrayColor;
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight)];
    [self.view addSubview:self.scrollView];
    self.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    CarView *carView = [[CarView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 80) andInteger:1];

    carView.backgroundColor = WhiteColor;
    [self.scrollView addSubview:carView];
    
    [self initDataSouce];
    [self initSubview];
    [self initBottomView];
    
    self.hud = [WKProgressHUD showInView:self.view withText:@"" animated:YES];
    
}

-(void)initDataSouce
{
    [Common requestWithUrlString:driver_info contentType:application_json finished:^(id responseObj) {
        
        if ([responseObj[@"data"] isKindOfClass:[NSNull class]]) {
            
            self.statusTextField.text = @"未认证";

            
        } else {
          
            self.dic = [NSMutableDictionary dictionaryWithDictionary:responseObj[@"data"]];
            
            if ([[self.dic allKeys]count ] > 0) {
                
                [self getValueString];
            }

        }
        
        [self.hud dismiss:YES];
        
        
    } failed:^(NSString *errorMsg) {
        
        [self.hud dismiss:YES];
        
        NSLog(@"%@",errorMsg);
    }];
}


-(void)getValueString
{
   
        
//    NSString *statusS = [NSString stringWithFormat:@"%@",self.dic[@"status"]];
        
    self.statusTextField.text = [self backString:self.dic[@"statusText"]];


    self.nameTextField.text = [self backString:self.dic[@"realName"]];
    
    if ([self.statusTextField.text isEqualToString:@"未认证"] || [self.statusTextField.text isEqualToString:@"认证未通过"] ) {
        
        self.idTextField.text = [self backString:self.dic[@"idno"]];

    } else {
        self.idTextField.text = [self backString:self.dic[@"showIdno"]];
 
    }
    
    [self.postDic setObject:self.nameTextField.text forKey:@"realName"];
    [self.postDic setObject:[self backString:self.dic[@"idno"]] forKey:@"idno"];
    [self.postDic setObject:[self backString:self.dic[@"idpic1"]] forKey:@"idpic1"];
    [self.postDic setObject:[self backString:self.dic[@"idpic2"]] forKey:@"idpic2"];
    [self.postDic setObject:[self backString:self.dic[@"id"]] forKey:@"id"];

    
    
    if ([self.dic[@"idpicUrl1"] isEqual:[NSNull null]]) {
        
    } else {
        NSURL *url = [NSURL URLWithString:self.dic[@"idpicUrl1"]];
        
        [self.firstImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"car_add"]];
//        [self.firstImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"car_add"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            
//            if (error != nil) {
//                
//                self.firstImg.image = [UIImage imageNamed:@"bad_picture"];
//            }
//            
//            
//        }];

    }
    
    if ([self.dic[@"idpicUrl2"] isEqual:[NSNull null]]) {
        
    } else {
        NSURL *url = [NSURL URLWithString:self.dic[@"idpicUrl2"]];
        
        
        [self.secondImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"car_add"]];
        
//        [self.secondImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"car_add"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            
//            if (error != nil) {
//                
//                self.secondImg.image = [UIImage imageNamed:@"bad_picture"];
//            }
//            
//            
//        }];
    }

    
    UILabel *bottomL = (UILabel *)[self.view viewWithTag:601];
    if ([self.dic[@"status"] integerValue] == 10 || [self.dic[@"status"] integerValue] == 30 )
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 64, screenWidth, CGRectGetMinY(bottomL.frame));
        
        [self.scrollView addSubview:button];
        [button addTarget:self action:@selector(tishiMessage) forControlEvents:UIControlEventTouchUpInside];
    }

    
}

-(NSString *)backString:(NSString *)string
{
    
    if ([string isEqual:[NSNull null]]) {
        return @"";
    } else {
        return string;
    }
}

-(void)initSubview
{
    //个人信息
    UIView *firstV = [self backViewWithFrame:90 andString:@"个人信息" andInteger:14 andColor:littleBlackColor];
    [self.scrollView addSubview:firstV];
    
    //认证状态
    UIView *view = [self backViewWithFrame:CGRectGetMaxY(firstV.frame) andString:@"认证状态" andInteger:13 andColor:fontGrayColor];
    [self.scrollView addSubview:view];
    
    self.statusTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.statusTextField.textAlignment = NSTextAlignmentRight;
    self.statusTextField.font = Font(13);
    [view addSubview:self.statusTextField];
    self.statusTextField.userInteractionEnabled = NO;
    self.statusTextField.textColor = RedColor;
    
    
    //姓名
    UIView *secondV = [self backViewWithFrame:CGRectGetMaxY(view.frame) andString:@"姓名" andInteger:13 andColor:fontGrayColor];
    
    [self.scrollView addSubview:secondV];
    
    self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.nameTextField.textAlignment = NSTextAlignmentRight;
    self.nameTextField.font = Font(13);
    self.nameTextField.delegate = self;
//    self.nameTextField.clearsOnBeginEditing = YES;
    self.nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.nameTextField.returnKeyType = UIReturnKeyDone;

    [secondV addSubview:self.nameTextField];
    self.nameTextField.textColor = carScrollColor;
    
    
    
    //籍贯
    UIView *thirdV = [self backViewWithFrame:CGRectGetMaxY(secondV.frame) andString:@"籍贯" andInteger:13 andColor:fontGrayColor];
    
    [self.scrollView addSubview:thirdV];
    
    self.driverNumberTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.driverNumberTF.textAlignment = NSTextAlignmentRight;
    self.driverNumberTF.font = Font(13);
    self.driverNumberTF.delegate = self;
    [thirdV addSubview:self.driverNumberTF];
    self.driverNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.driverNumberTF.textColor = carScrollColor;
    self.driverNumberTF.returnKeyType = UIReturnKeyDone;

  
    
    //身份证号
    UIView *fourV = [self backViewWithFrame:CGRectGetMaxY(thirdV.frame) andString:@"身份证号" andInteger:13 andColor:fontGrayColor];
    
    [self.scrollView addSubview:fourV];
    
    self.idTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.idTextField.textAlignment = NSTextAlignmentRight;
    self.idTextField.font = Font(13);
    self.idTextField.delegate = self;
    [fourV addSubview:self.idTextField];
    self.idTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.idTextField.textColor = carScrollColor;
    self.idTextField.returnKeyType = UIReturnKeyDone;
    
    
    //出生日期
    
    UIView *fiveV = [self backViewWithFrame:CGRectGetMaxY(fourV.frame) andString:@"出生日期" andInteger:13 andColor:fontGrayColor];
    
    [self.scrollView addSubview:fiveV];
    
    self.driverDateTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.driverDateTF.textAlignment = NSTextAlignmentRight;
    self.driverDateTF.font = Font(13);
    self.driverDateTF.delegate = self;
    [fiveV addSubview:self.driverDateTF];
    self.driverDateTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.driverDateTF.textColor = carScrollColor;
    self.driverDateTF.returnKeyType = UIReturnKeyDone;
    
    
    //家庭住址
    
    UIView *sixV = [self backViewWithFrame:CGRectGetMaxY(fiveV.frame) andString:@"家庭住址" andInteger:13 andColor:fontGrayColor];
    
    [self.scrollView addSubview:sixV];
    
    self.addressTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 0, screenWidth - 120 - 15, cellHeight)];
    self.addressTF.textAlignment = NSTextAlignmentRight;
    self.addressTF.font = Font(13);
    self.addressTF.delegate = self;
    [sixV addSubview:self.addressTF];
    self.addressTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.addressTF.textColor = carScrollColor;
    self.addressTF.returnKeyType = UIReturnKeyDone;
    
    
    UIView *sevenV = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(sixV.frame), screenWidth, 105 )];
    sevenV.backgroundColor = WhiteColor;
    
    [self.scrollView addSubview:sevenV];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(18, 30 * kHeight, 100, 20 * kHeight)];
    label.text = @"身份证照片";
    label.textColor = fontGrayColor;
    label.font = Font(13);
    [sevenV addSubview:label];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(18,  CGRectGetMaxY(label.frame), 150, 15 * kHeight)];
    label2.text = @"(需完整清晰无遮挡)";
    label2.textColor = YellowColor;
    label2.font = Font(11);
    [sevenV addSubview:label2];
    
    
    self.firstImg = [[UIImageView alloc]init];
    self.secondImg = [[UIImageView alloc]init];
    self.idFirstButton = [self backButttonWithImageV:self.firstImg AndString:@"正面" andFloat:screenWidth - 178];
    [sevenV addSubview:self.idFirstButton];
    [self.idFirstButton addTarget:self action:@selector(idFirstButtons) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.idSecondButton = [self backButttonWithImageV:self.secondImg AndString:@"反面" andFloat:screenWidth - 88];
    [sevenV addSubview:self.idSecondButton];
    [self.idSecondButton addTarget:self action:@selector(idSecondButtons) forControlEvents:UIControlEventTouchUpInside];

    [self initToolBar];
    
}


-(UIButton *)backButttonWithImageV:(UIImageView *)img AndString:(NSString *)string andFloat:(CGFloat)x
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(x, 7 * kHeight, 70, 90);
    img.frame = CGRectMake(0, 0, 70, 70);
    img.image = [UIImage imageNamed:@"car_add"];
    
    [button addSubview:img];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(img.frame) + 2.5, 70, 20)];
    label.textColor = carScrollColor;
    label.font = Font(12);
    label.text = string;
    label.textAlignment = NSTextAlignmentCenter;
    [button addSubview:label];
    
    return  button;

}


-(void)initBottomView
{
    
    UILabel *bottomL = [[UILabel alloc]initWithFrame:CGRectMake(18, 84 + cellHeight * 7 + 100 , screenWidth, cellHeight)];
    bottomL.textColor = YellowColor;
    bottomL.text = @"认证中和认证通过信息不能修改";
    bottomL.font = Font(10);
    bottomL.tag = 601;
    
    [self.scrollView addSubview:bottomL];
    
     if ([self.dic[@"status"] integerValue] == 10 || [self.dic[@"status"] integerValue] == 30 )
     {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, screenWidth, CGRectGetMinY(bottomL.frame));
    
    [self.scrollView addSubview:button];
    [button addTarget:self action:@selector(tishiMessage) forControlEvents:UIControlEventTouchUpInside];
     }
         
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame = CGRectMake(14, CGRectGetMaxY(bottomL.frame), screenWidth - 28, Button_height);
    nextButton.backgroundColor = YellowColor;
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextButton setTitleColor:WhiteColor forState:UIControlStateNormal];
    nextButton.layer.cornerRadius = 5;
    [nextButton addTarget:self action:@selector(nextButton) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.scrollView.contentSize = CGSizeMake(screenWidth, CGRectGetMaxY(nextButton.frame) + 20 + 64);
    
    [self.scrollView addSubview:nextButton];
}

-(void)tishiMessage
{
    [WKProgressHUD popMessage:@"认证中和认证通过信息不能修改" inView:self.view duration:1.5 animated:YES];

}


-(void)nextButton
{
    
    self.number = 0;
    
    if ([self.statusTextField.text isEqualToString:@"未认证"] || [self.statusTextField.text isEqualToString:@"认证未通过"]) {
        
    Common *c = [[Common alloc]init];
        
        
    [c afPostRequestWithUrlString:driver_info_save parms:self.postDic finishedBlock:^(id responseObj) {
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        
        if (_number != 0) {
            
             [WKProgressHUD popMessage:dic[@"message"] inView:self.view duration:1.5 animated:YES];
        }
       

        if ([dic[@"success"] boolValue]) {
            
            PerfectCarInfoVC2 *perfect2 = [[PerfectCarInfoVC2 alloc]init];
            [self.navigationController pushViewController:perfect2 animated:YES];
        }
        
    } failedBlock:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    } else {
        
        PerfectCarInfoVC2 *perfect2 = [[PerfectCarInfoVC2 alloc]init];
        [self.navigationController pushViewController:perfect2 animated:YES];
    }
   
}


-(UIView *)backViewWithFrame:(CGFloat)y andString:(NSString *)string andInteger:(NSInteger)integer andColor:(UIColor *)color
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, y, screenWidth, cellHeight)];
    
    UILabel *nameL = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 100, cellHeight)];
    nameL.textColor = color;
    nameL.font = Font(integer);
    nameL.text = string;
    [view addSubview:nameL];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, cellHeight - 0.5, screenWidth, 0.5)];
    label.backgroundColor = LineGrayColor;
    
    [view addSubview:label];
    view.backgroundColor = WhiteColor;
    
    return  view;
}

-(void)viewWillAppear:(BOOL)animated
{
    
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
    
    if (self.number != 1) {
        [self initDataSouce];

    }
    
}


-(void)initToolBar
{
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,screenWidth, 35 * kHeight)];
    toolBar.backgroundColor = Color_RGB(248, 230, 208, 1);
    //    [self.view addSubview:toolBar];
    
    UIBarButtonItem *btnPlace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    UIBarButtonItem *lastButton = [[UIBarButtonItem alloc]initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(lastOneFirstResponser:)];
    
    [lastButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * kHeight], NSFontAttributeName,BlackColor,NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(nextOneFirstResponder:)];
    
    [nextButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * kHeight], NSFontAttributeName,BlackColor,NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(keyBoardExit:)];
    
    [doneButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * kHeight], NSFontAttributeName,BlackColor,NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
    
    NSArray *itemsArray = @[lastButton,nextButton,btnPlace,doneButton];
    toolBar.items = itemsArray;
    
    self.nameTextField.inputAccessoryView = toolBar;
    self.driverNumberTF.inputAccessoryView = toolBar;
    self.idTextField.inputAccessoryView = toolBar;
    self.driverDateTF.inputAccessoryView = toolBar;
    self.addressTF.inputAccessoryView = toolBar;

    
    self.textFieldArray = [NSMutableArray arrayWithObjects:self.nameTextField,self.driverNumberTF,self.idTextField,self.driverDateTF,self.addressTF ,nil];
    
}

-(void)nextOneFirstResponder:(UIBarButtonItem *)sender
{
    if (_currentTextFieldIndex + 1 <= self.textFieldArray.count - 1) {
        UITextField *textField = self.textFieldArray[_currentTextFieldIndex + 1];
        [textField becomeFirstResponder];
    }
}

-(void)lastOneFirstResponser:(UIBarButtonItem *)sender
{
    if (_currentTextFieldIndex - 1 >= 0) {
        UITextField *textField = self.textFieldArray[_currentTextFieldIndex - 1];
        [textField becomeFirstResponder];
    }
}

-(void)keyBoardExit:(UIBarButtonItem *)sender
{
    [self.view endEditing:YES];
}


#pragma mark 输入框

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    if (textField == self.nameTextField) {
        _currentTextFieldIndex = 0;
    }
    
    if (textField == self.driverNumberTF) {
        _currentTextFieldIndex = 1;
    }
    
    if (textField == self.idTextField) {
        _currentTextFieldIndex = 2;
        
    }
    
    if (textField == self.driverDateTF) {
        _currentTextFieldIndex = 3;
        
    }
    
    if (textField == self.addressTF) {
        _currentTextFieldIndex = 4;
        
    }


    //获取相对于window的坐标
    
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[textField convertRect: textField.bounds toView:window];
        
    if (rect.origin.y + 364 + cellHeight > screenHeight) {
        
        [UIView animateWithDuration:0.3 animations:^{
            self.scrollView.contentInset = UIEdgeInsetsMake(-(rect.origin.y + 364 + cellHeight- screenHeight), 0, 0, 0);

        }];
        
        
    }
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
    }];
    
    if (textField == self.nameTextField) {
        [self.postDic setObject:self.nameTextField.text forKey:@"realName"];
    }
    
    if (textField == self.idTextField) {
        
        [self.postDic setObject:self.idTextField.text forKey:@"idno"];
    }

}




//点击return 按钮 去掉
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark 获取图片

-(void)idFirstButtons
{
    [self buttonToGetPicture];
    self.integer = 1;
}

-(void)idSecondButtons
{
    [self buttonToGetPicture];
    self.integer = 2;
}


#pragma mark 获取图片
-(void)buttonToGetPicture
{
    
    __weak typeof(self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf getPictureFromCamera];
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf getPictureFromLibrary];
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark 拍照
-(void)getPictureFromCamera
{
    self.number = 1;
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark 从相册选取
-(void)getPictureFromLibrary
{
    self.number = 1;
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        if (self.integer == 1) {
            self.firstImg.image = image;
        } else {
            self.secondImg.image = image;
        }
//        self.imageV.image = image;
        
        self.hud = [WKProgressHUD showInView:self.view withText:@"" animated:YES];
        
        //上传服务器
        [self getTokenFromQN];
        
    }];
}


-(NSData *)getImageWith:(UIImage *)image
{
    NSData *data = nil;
    if (UIImagePNGRepresentation(image)) {
        
        data = UIImageJPEGRepresentation(image, 0.3);
        
        
    } else {
        data = UIImagePNGRepresentation(image);
        
        
    }
    
    return data;
}

#pragma mark 从七牛获取Token
-(void)getTokenFromQN
{
    //    NSString *string = [NSString stringWithFormat:@"%@qiniu/upload/ticket",Main_interface];
    
    [Common requestWithUrlString:qiniu_token contentType:application_json finished:^(id responseObj) {
        if ([responseObj[@"success"] boolValue]) {
            
            if (self.integer == 1) {
                self.token1 = responseObj[@"data"];
                [self uploadImageToQNWithData:[self getImageWith:self.firstImg.image] andToken:self.token1];

            }else {
                self.token2 = responseObj[@"data"];
                [self uploadImageToQNWithData:[self getImageWith:self.secondImg.image] andToken:self.token2];

                
            }
        }
    } failed:^(NSString *errorMsg) {
        
        [self.hud dismiss:YES];

        NSLog(@"%@",errorMsg);
    }];
    
    
}

-(void)uploadImageToQNWithData:(NSData *)data andToken:(NSString *)string
{

    QNUploadManager *upManager = [[QNUploadManager alloc]init];
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
        
    } params:nil checkCrc:NO cancellationSignal:nil];
    
    [upManager putData:data key:nil token:string complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        if (self.integer == 1) {
            [self.postDic setObject:resp[@"key"] forKey:@"idpic1"];
        } else {
            [self.postDic setObject:resp[@"key"] forKey:@"idpic2"];
        }
        
        [self.hud dismiss:YES];
        
    } option:uploadOption];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
